require(["jquery",'public','weui'],function($,public,weui) {
    //public.headertitle("找回密码");
    //public.footer(4);
    public.allocator();

    //页面加载完毕
    public.loaded();

    //重置密码
	$('.sign_in_main').on('click','.reset_password',function(event){
		event.preventDefault();
		var $this = $(this);
		if($this.hasClass('on')){
			return false;
		}
		var data = {
			user_phone: $('#project_mobile_numeber').val(),
			verification_code: $('#verification_code').val(),
			user_password: $('.project_password').val().trim(),
			project_password_repeat: $('.project_password_repeat').val().trim(),
		};
		
		var phone_rep = /^1[3-9]\d{9}$/;
		if(!phone_rep.test(data.user_phone)){
			$.alert('请输入可用的手机号码');
			return false;
		}
		var code_rep = /^[\d|\w]{2,8}$/;
		if(!code_rep.test(data.verification_code)){
			$.alert('验证码错误');
			return false;
		}		
		if(data.user_password.length != 6){
			$.alert('密码长度必须为6');
			return false;
		}
		if(data.user_password !== data.project_password_repeat){
			$.alert('两次输入的密码不匹配');
			return false;
		}

		$this.addClass('on');
		$.ajax({
            type: 'post',
            dataType: 'json',
            data: data,
            url: MODULE+'Index/reset_password',
        }).success(function (res) {
            console.log(res);   
            if(res.status == 1){//成功
            	$.alert(res.info,function(){
            		window.location.replace(MODULE+'PublicJump/sign');
            	});
            }else{
            	$.alert(res.info);
            }
        }).fail(function (jqXHR, textStatus, errorThrown) {
            $.alert('重置失败');
            $this.removeClass('on');            
        });
	});

	//获取验证码
	$('.sign_in_main').on('click','#send_code',function(event){
		event.preventDefault();
		var $this = $(this);
		if($this.hasClass('on')){
			return false;
		}
		var project_mobile_numeber = $('#project_mobile_numeber').val();
		var regexp = /^1[3-9]\d{9}$/;
		if(!regexp.test(project_mobile_numeber)){
			$.alert('请输入可用的手机号码');
			return false;
		}
		$this.addClass('on');
		$.ajax({
            type: 'post',
            dataType: 'json',
            data: {user_phone: project_mobile_numeber},
            url: MODULE+'Index/_sendsms',
        }).success(function (res) {
            console.log(res);   
            if(res.code == 0){
            	$.alert('已发送');
            	countdown();
            }else{
            	$.alert('发送失败');
            }
        }).fail(function (jqXHR, textStatus, errorThrown) {
            $.alert('发送失败');
            $this.removeClass('on');            
        });
	});

	function countdown(){
		var num = 60;
		var $btn = $('#send_code');
		var $txt = $('#send_code').siblings('span');
		$btn.hide();
		$txt.show();
		var timer = setInterval(function(){
			num --;
			if(num < 0){
				clearInterval(timer);
				$btn.show().removeClass('on');
				$txt.hide();
				return false;
			}
			$txt.text(num+'秒');
		},1000);
	}
})